package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemEnterpriseSingleVideoBinding;
import com.sgb.kjwl.databinding.NeedNewItemBinding;
import com.sgb.kjwl.model.entity.enterprise.ListBean;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsInfoVideoAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者:黄淋
 * 日期:2021/5/20
 * 说明:需求item
 */
public class HomeNeedAdapter extends BaseMultiItemQuickAdapter<ListBean, BaseViewHolder> {

    RequestOptions options = new RequestOptions().placeholder(R.mipmap.icon_default_company)//图片加载出来前，显示的图片
                .fallback(R.mipmap.icon_default_company) //url为空的时候,显示的图片
                .error(R.mipmap.icon_default_company);//图片加载失败后，显示的图片

    RequestOptions personOptions = new RequestOptions().placeholder(R.mipmap.morentu_person)//图片加载出来前，显示的图片
                .fallback(R.mipmap.morentu_person) //url为空的时候,显示的图片
                .error(R.mipmap.morentu_person);//图片加载失败后，显示的图片

    RequestOptions teamOptions = new RequestOptions().placeholder(R.mipmap.icon_default_team)//图片加载出来前，显示的图片
                .fallback(R.mipmap.icon_default_team) //url为空的时候,显示的图片
                .error(R.mipmap.icon_default_team);//图片加载失败后，显示的图片

    public HomeNeedAdapter(List<ListBean> datas) {
        super(datas);
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.need_new_item);
        //视频类别
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_enterprise_video_list);
        //单个视频类别
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.item_enterprise_single_video);
    }

    @Override
    protected void convert(BaseViewHolder helper, ListBean item) {
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                setNeed(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                initVideoList(helper, item);
                break;
            case SingleItemConstant.TYPE_THREE:
                initSingleVideo(helper, item);
                break;
        }
    }

    private void setNeed(BaseViewHolder holder, ListBean item) {
        NeedNewItemBinding binding = DataBindingUtil.bind(holder.itemView);
        binding.setViewModel(item);
        String KeyWord;
        String s = item.offerPerson + "";
        if (s.length() == 5) {
            KeyWord = getString(s, 1, 2, 3);
        } else if (s.length() == 6) {
            KeyWord = getString(s, 2, 3, 4);
        } else if (s.length() == 7) {
            KeyWord = getString(s, 3, 4, 5);
        } else if (s.length() == 8) {
            KeyWord = getString(s, 4, 5, 6);
        } else {
            KeyWord = item.offerPerson + "";
        }

        if (item.offerStatus == 1) {
            Glide.with(mContext).load(R.mipmap.icon_baojiazhong).into(binding.imageStateBaojia);
        } else if (item.offerStatus == 2) {
            Glide.with(mContext).load(R.mipmap.icon_yijieshu).into(binding.imageStateBaojia);
        }
        if (!TextUtils.isEmpty(item.servicePrincipal) && item.requirementTypes > 4) {
            holder.getView(R.id.image_q).setVisibility(item.servicePrincipal.indexOf("1") > -1 ? View.VISIBLE : View.GONE);
            holder.getView(R.id.image_t).setVisibility(item.servicePrincipal.indexOf("2") > -1 ? View.VISIBLE : View.GONE);
//            Log.e("showlogo:"+ UiUtil.getTime(item.updateTime), holder.getView(R.id.image_q).getVisibility()+"___"+holder.getView(R.id.image_t).getVisibility());
        } else {
            holder.getView(R.id.image_q).setVisibility(View.GONE);
            holder.getView(R.id.image_t).setVisibility(View.GONE);
        }
        holder.setText(R.id.tv_need_type, item.requirementTypes == 1 ? "商标需求" : item.requirementTypes == 2 ? "专利需求" :
                item.requirementTypes == 3 ? "版权需求" : item.requirementTypes == 5 ? "服务需求" : "需求")
                .setText(R.id.tv_count_all, KeyWord);
        if (item.publishType == 1) {
            binding.rivNeedLogo.setVisibility(View.GONE);
            binding.ivNeedItemLogo.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(item.image).apply(options).into(binding.ivNeedItemLogo);
        } else if (item.publishType == 2) {
            binding.rivNeedLogo.setVisibility(View.VISIBLE);
            binding.ivNeedItemLogo.setVisibility(View.GONE);
            Glide.with(mContext).load(item.image).apply(teamOptions).into(binding.rivNeedLogo);
        } else {
            binding.rivNeedLogo.setVisibility(View.VISIBLE);
            binding.ivNeedItemLogo.setVisibility(View.GONE);
            Glide.with(mContext).load(item.image).apply(personOptions).into(binding.rivNeedLogo);
        }
    }

    /**
     * 设置视频信息
     */
    private void initVideoList(BaseViewHolder helper, ListBean entity) {
        List<VideoBean> videoBeanList = new ArrayList<>();
        videoBeanList.addAll(entity.getVideos());
        RecyclerView recyclerView = helper.getView(R.id.list_data);
        if (entity.getVideos() == null || entity.getVideos().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            return;
        }
        recyclerView.setVisibility(View.VISIBLE);
        if (recyclerView.getAdapter() == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            GoodsInfoVideoAdapter adapter = new GoodsInfoVideoAdapter(mContext, videoBeanList);
            adapter.setOnItemClick(new AdapterOnItemClick<VideoBean>() {
                @Override
                public void onItemClick(VideoBean homeHostVideoEntity, int position) {
                    Intent intent = new Intent(mContext, PlayListActivity.class);
                    intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) adapter.getDatas());
                    intent.putExtra(PlayListActivity.PSO, position);
                    intent.putExtra(PlayListActivity.VIDEOTYPE, "goods_and_car_video");
                    mContext.startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter);
        } else {
            ((GoodsInfoVideoAdapter) recyclerView.getAdapter()).setDatas(videoBeanList);
        }
    }

    /**
     * 设置单个视频信息
     */
    private void initSingleVideo(BaseViewHolder helper, ListBean entity) {
        //设置dataBinding数据
        ItemEnterpriseSingleVideoBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        if (entity.getVideos() != null && !entity.getVideos().isEmpty()) {
            binding.setVariable(BR.viewModel, entity.getVideos().get(0));
            binding.executePendingBindings();
        }
        binding.ivItemCarHallVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, PlayListActivity.class);
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) entity.getVideos());
                intent.putExtra(PlayListActivity.PSO, 0);
                intent.putExtra(PlayListActivity.VIDEOTYPE, "goods_and_car_video");
                mContext.startActivity(intent);
            }
        });
    }

    private String getString(String s, int i2, int i3, int i4) {
        String KeyWord;
        int pos1 = Integer.parseInt(s.substring(0, i2));
        int pos2 = Integer.parseInt(s.substring(i2, i3));
        int pos3 = Integer.parseInt(s.substring(i3, i4));
        if (pos3 >= 5) pos2++;
        if (pos2 == 10) {
            pos1++;
        }
        KeyWord = pos2 == 10 ? pos1 + ".0w" : pos1 + "." + pos2 + "w";
        return KeyWord;
    }

}
